using System;
using System.Collections.Generic;
using System.Text;

using System.ComponentModel;
using System.ComponentModel.Design;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using Husb.Util;

namespace Husb.WebControls
{
    public class WhereClauseQueryStringParameter : QueryStringParameter
    {
        public WhereClauseQueryStringParameter()
        { }
        protected WhereClauseQueryStringParameter(WhereClauseQueryStringParameter original):base(original)
        {
        }

        public WhereClauseQueryStringParameter(string name, string queryStringField, string dataField)
            : base(name, queryStringField)
        {
            this.AdditionalCriteria = "";
            this.DataField = dataField;
            this.OperatorKind = ExpressionKind.Equals;
        }

        public WhereClauseQueryStringParameter(string name, string queryStringField, string dataField, string additionalCriteria)
            : base(name, queryStringField)
        {
            this.AdditionalCriteria = additionalCriteria;
            this.DataField = dataField;
            this.OperatorKind = ExpressionKind.Equals;
        }

        public WhereClauseQueryStringParameter(string name, string queryStringField, string dataField, ExpressionKind operatorKind)
            : base(name, queryStringField)
        {
            this.AdditionalCriteria = "";
            this.DataField = dataField;
            this.OperatorKind = operatorKind;
        }

        public WhereClauseQueryStringParameter(string name, string queryStringField, string dataField, ExpressionKind operatorKind, string additionalCriteria)
            : base(name, queryStringField)
        {
            this.AdditionalCriteria = additionalCriteria;
            this.DataField = dataField;
            this.OperatorKind = operatorKind;
        }

        //public WhereClauseQueryStringParameter(string name, DbType dbType, string queryStringField, string dataField)
        //    : base(name, dbType, queryStringField)
        //{
        //    this.DataField = dataField;
        //}

        public WhereClauseQueryStringParameter(string name, TypeCode type, string queryStringField, string dataField)
            : base(name, type, queryStringField)
        {
            this.AdditionalCriteria = "";
            this.DataField = dataField;
            this.OperatorKind = ExpressionKind.Equals;
        }

        public WhereClauseQueryStringParameter(string name, TypeCode type, string queryStringField, string dataField, ExpressionKind operatorKind)
            : base(name, type, queryStringField)
        {
            this.AdditionalCriteria = "";
            this.DataField = dataField;
            this.OperatorKind = operatorKind;
        }

        public WhereClauseQueryStringParameter(string name, TypeCode type, string queryStringField, string dataField, ExpressionKind operatorKind, string additionalCriteria)
            : base(name, type, queryStringField)
        {
            this.AdditionalCriteria = additionalCriteria;
            this.DataField = dataField;
            this.OperatorKind = operatorKind;
        }

        protected override Parameter Clone()
        {
            return base.Clone();
        }

        protected override object Evaluate(System.Web.HttpContext context, Control control)
        {
            string s = base.Evaluate(context, control) as String;
            if (s == null) return null;

            string returnVal = "";
            if (OperatorKind == ExpressionKind.Like)
            {
                returnVal = DataField + " LIKE '%" + s + "%'";
            }

            returnVal = DataField + " = '" + s + "'";

            if (!String.IsNullOrEmpty(AdditionalCriteria))
            {
                returnVal += " AND " + AdditionalCriteria;
            }

            return returnVal;
        }

        public string DataField
        {
            get
            {
                object obj2 = base.ViewState["DataField"];
                if (obj2 == null)
                {
                    return string.Empty;
                }
                return (string)obj2;
            }
            set
            {
                if (this.DataField != value)
                {
                    base.ViewState["DataField"] = value;
                    base.OnParameterChanged();
                }
            }
        }

        public string AdditionalCriteria
        {
            get
            {
                object obj2 = base.ViewState["AdditionalCriteria"];
                if (obj2 == null)
                {
                    return string.Empty;
                }
                return (string)obj2;
            }
            set
            {
                if (this.DataField != value)
                {
                    base.ViewState["AdditionalCriteria"] = value;
                    base.OnParameterChanged();
                }
            }
        }

        public ExpressionKind OperatorKind
        {
            get
            {
                object obj2 = base.ViewState["OperatorKind"];
                if (obj2 == null)
                {
                    return ExpressionKind.Equals;
                }
                return (ExpressionKind)obj2;
            }
            set
            {
                if (this.OperatorKind != value)
                {
                    base.ViewState["OperatorKind"] = value;
                    base.OnParameterChanged();
                }
            }
        }

    }
}
